<?php


namespace app\api\controller;


use app\service\model\CategoryModel;
use app\service\model\GoodsModel;
use app\service\Y;

class Category extends BaseApi
{
    /**
     * 首页推荐分类
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function recommend_list(): \think\response\Json
    {
        $categories = CategoryModel::where('is_recommend', '=', 1)->limit(3)->select();
        foreach ($categories as $cate) {
            $cate['goods'] = $cate->goods()->with('images')
                ->limit(5)
                ->select();
        }
        return Y::success($categories, '首页推荐分类');
    }

    /**
     * 分类列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function cate_list(): \think\response\Json
    {
        $data = CategoryModel::select();
        return Y::success($data, '分类列表');
    }

    /**
     * 根据分类级别获取列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function get_level_cate_list(): \think\response\Json
    {
        $level = input('param.level', 0);
        if (!$level) {
            return Y::error('参数错误');
        }
        $data = CategoryModel::where('level', '=', $level)->select();
        return Y::success($data, '分类列表');
    }
}